﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using System.IO;
using Emico.Linq.Criterion;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;

namespace Emico.Core.Tests.Criterion
{
    [TestClass]
    public class InExpressionTest
    {
        [TestMethod]
        public void TestSerialize()
        {
            string propertyName = "Id";
            int[] values = new int[]
            {
                1, 2, 3
            };

            InExpression<TestType, int> e = new InExpression<TestType, int>(propertyName, values);
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, e);

                ms.Position = 0;
                var result = (InExpression<TestType, int>)bf.Deserialize(ms);

                Assert.AreEqual(values.Length, result.Values.Count());
                Assert.AreEqual(propertyName, result.PropertyName);
            }
        }

        [TestMethod]
        public void TestQuery()
        {
            TestType[] data = new TestType[]
            {
                new TestType { Id = 1 },
                new TestType { Id = 2 },
                new TestType { Id = 3 },
                new TestType { Id = 4 },
                new TestType { Id = 5 },
                new TestType { Id = 6 }
            };

            int[] values = new int[]
            {
                1, 2
            };

            InExpression<TestType, int> e = new InExpression<TestType, int>(t => t.Id, values);

            Criteria<TestType> criteria = new Criteria<TestType>(e);
            int count = criteria.AsQueryable(data).Count();
            Assert.AreEqual(2, count);            
        }
    }
}
